package demo5;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-07-07
 * Time: 22:36
 */
public class Test {
    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 10;


    /**
     * 打印顺序表:
     *   根据usedSize判断即可
     */
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i]+" ");
        }
        System.out.println();
    }

    // 新增元素,默认在数组最后新增
    private void capa(){
        this.elem = Arrays.copyOf(elem,elem.length * 2);
    }
    public void add(int data) {
        if(isFull()){
            capa();
        }
        this.elem[usedSize] = data;
        usedSize++;
    }

    /**
     * 判断当前的顺序表是不是满的！
     * @return true:满   false代表空
     */
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }


    private boolean checkPosInAdd(int pos) {
        if(pos < 0 || pos > this.usedSize){
            throw new IsFullException("pos下标不合法");
        }
        return true;//合法
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(!checkPosInAdd(pos)){
            throw new IsFullException("pos下标不合法");
        }
        if(isFull()){
            capa();
        }
        for (int i = this.usedSize-1; i >= pos ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < elem.length; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < elem.length; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if(!checkPosInAdd(pos)){
            throw new IsFullException("pos下标不合法");
        }
        return elem[pos];
    }

    private boolean isEmpty() {
        return elem == null;
    }
    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        if(!checkPosInAdd(pos)){
            throw new IsFullException("pos下标不合法");
        }
        if(isEmpty()){
            throw new IsFullException("顺序表为空");
        }
        elem[pos] = value;
    }

    /**
     * 删除第一次出现的关键字key
     * @param key
     */
    public void remove(int key) {
        if(isEmpty()){
            throw new IsFullException("数据表为空 不能删除");
        }
        int index = indexOf(key);
        if(index == -1) {
            System.out.println("不存在你要删除的数据");
            return;
        }

        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        //删除完成
        this.usedSize--;

    }

    // 获取顺序表长度
    public int size() {
        int count = 0;
        for (int i = 0; i < elem.length; i++) {
            count++;
        }
        return count;
    }

    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}
